mui.init({
	preloadPages: [ //缓存其他页面 
		{
			url: 'home.html',
			id: 'home.html',
			styles: {
				top: '0px',
				bottom: '50px'
			}
		},
		{
			url: 'function.html',
			id: 'function.html',
			styles: {
				top: '0px',
				bottom: '50px'
			}
		},
		{
			url: 'message_list.html',
			id: 'message_list.html',
			styles: {
				top: '0px',
				bottom: '50px'
			}
		},
		{
			url: 'user_center.html',
			id: 'user_center.html',
			styles: {
				top: '0px',
				bottom: '50px'
			}
		}
	],
	hardwareAccelerated: true, //开启硬件加速
	swipeBack: true //启用右滑关闭功能
});
mui.plusReady(function() {
	//console.log("所有plus api都应该在此事件发生后调用，否则会出现plus is undefined。"
	if(mui.os.android) {
		mui.back = function() {
			if(activeTab != 0){
				change(0);
				return true;
			}
			var btn = ["退出", "取消"];
			mui.confirm('您确定要退出应用吗？', '退出确认', btn, function(e) {
				if(e.index == 0) {
					plus.runtime.quit();
					PluginWrapper.exit();
				}
			});
		};
	};
	//隐藏滚动条
	plus.webview.currentWebview().setStyle({
		scrollIndicator: 'none'
	});

	change(activeTab);

})

//当前激活选项
var activeTab = 0;
var subpageIds = ["home.html", "message_list.html", "function.html", "user_center.html"];
var focusImages = ['../img/tab_index_focus.png', '../img/tab_message_focus.png', '../img/tab_work_focus.png', '../img/tab_owner_focus.png'];
var normalImages = ['../img/tab_index_normal.png', '../img/tab_message_normal.png', '../img/tab_work_normal.png', '../img/tab_owner_normal.png'];

/*
 @function	切换tab指示器
 * */
function change(targetTab) {
	if(!plus.webview.getWebviewById(subpageIds[targetTab])) {
		var sub = plus.webview.create(subpageIds[targetTab], subpageIds[targetTab], {
			top: '0px',
			bottom: '50px',
			hardwareAccelerated: true //开启硬件加速
		}, {
			returnFlag: false
		});
		sub.hide();
		var self = plus.webview.currentWebview();
		self.append(sub);
	}
	//显示目标选项卡
	plus.webview.show(subpageIds[targetTab], "none");
	//高亮选项卡指示器
//	document.getElementById(targetTab).getElementsByTagName("img")[0].setAttribute('src', focusImages[targetTab]);
	if(targetTab != activeTab) {
		//黑暗当前指示器
//		document.getElementById(activeTab).getElementsByTagName("img")[0].setAttribute('src', normalImages[activeTab]);
		//隐藏当前;
		plus.webview.hide(subpageIds[activeTab]);
	}
	//更改当前活跃的选项卡
	activeTab = targetTab;
}
//选项卡点击事件
mui('.mui-bar-tab').on('tap', 'a', function(e) {
	var targetTab = parseInt(this.id);
	if(targetTab == activeTab) {
		return;
	}
	change(targetTab);
});